<template>
  <ex-dialog width="600px" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <p style="margin: 0 10px 14px">订单编号：{{id}}</p>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :label-width="labelWidth" class="demo-ruleForm">
        <el-form-item label="退款金额：" prop="refundMoney">
          <el-input v-model="ruleForm.refundMoney" placeholder="请输入退款金额" :style="{width: inputWidth}"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="button_box">
      <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
    <el-dialog width="500px" title="确认退款" :visible.sync="innerVisible" append-to-body>
      <p>是否将支付款项 {{ruleForm.refundMoney}} 原路退回至用户 {{phone}} 的微信账户</p>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="AgreeLoading" @click="submitRefund">确认退回</el-button>
        <el-button @click="innerVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </ex-dialog>
</template>
<script>
  import Loading from "@/components/Loading/index";
  // api
  import {
    couponRefund,
    couponConfirmRefund,
  } from "@/api/consumeMiningManagement/punchVerificationRecord";

  export default {
    components: {
      Loading,
    },
    props: {
      visible: Boolean,
      id: String,
    },
    data() {
      var checkRefundMoney = (rule, value, callback) => { //券有效期限制时间校验
        let regNumber = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/; //大于0的数值且最多保留两位小数的数值
        if (regNumber.test(value) == false) {
          return callback(new Error('请输入大于0且最多保留两位小数的数值'));
        } else {
          return callback()
        }
      };
      return {
        labelWidth: '100px',
        inputWidth: '300px',
        title: '退款',
        loading: false,
        disabled: false,
        AgreeLoading: false, //按钮loading
        innerVisible: false,
        phone:'',//退款手机号
        ruleForm: {
          orderNo: '', //订单编号
          refundMoney: '', //退款金额
        },
        rules: {
          refundMoney: [{
            required: true,
            validator: checkRefundMoney,
            trigger: ['change', 'bulr'],
          }],
        },
      };
    },
    computed: {},
    created() {
      console.log(this.id)
      this.ruleForm.orderNo = this.id
      // if (this.id != null) {
      //   this.title = '编辑'
      //   this.disabled = true
      //   this.passwordIsShow = false
      //   this.getInfo()
      // }

    },
    mounted() {

    },
    methods: {
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
      getInfo() { //获取信息
        couponRefund(this.ruleForm).then((res) => {
            this.phone = res.result
            this.innerVisible = true
          })
      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.getInfo()
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      submitRefund() { //确认退回
        this.AgreeLoading = true
        var params = {
          ...this.ruleForm,
        };
        couponConfirmRefund(params).then((res) => {
            this.$message({
              message: "操作成功",
              type: "success",
            });
            setTimeout(() => {
              this.$emit('success')
              this.handleClose()
              // setTimeout(() => {
              //   this.AgreeLoading = false
              // }, 2000)
              this.AgreeLoading = false
            }, 1000);
          })
          .catch(() => {
            this.AgreeLoading = false;
          });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }

  .button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }

</style>
